//
//  UIViewController+JKExtension.m
//  Category
//
//  Created by jamalping on 16/4/6.
//  Copyright © 2016年 jamalping. All rights reserved.
//

#import "UIViewController+JKExtension.h"

@implementation UIViewController (JKExtension)

- (void)setTitle:(NSString *)title {
    
    NSDictionary *attributes=[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,[UIFont boldSystemFontOfSize:16],NSFontAttributeName, nil];
    
    [self.navigationController.navigationBar setTitleTextAttributes:attributes];
    
    self.navigationItem.title = title;
}

- (void)setBackBarButtonItemTitle:(NSString *)title {
    if (title) {
        NSLog(@"self.title = %@",title);
        self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:self action:nil];
    }
}

-(UIButton *)customBackButtonItemWithTitle:(NSString *)title image:(NSString *)imageName
{
    
    UIButton *backbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backbutton setTitle:title forState:UIControlStateNormal];
    [backbutton setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [backbutton sizeToFit];
    UIBarButtonItem *backBarbuttonItem = [[UIBarButtonItem alloc] initWithCustomView:backbutton];
    self.navigationItem.leftBarButtonItem = backBarbuttonItem;
    
    [backbutton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    return backbutton;
}

-(void)backAction
{
    NSLog(@"%@",self.navigationController.presentingViewController);
    if (self.navigationController.presentingViewController&&self.navigationController.viewControllers.count<=1) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

/**
 * @brief  添加单击手势
 */
- (void)addEndEditingTapGestureRecognizer {
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cancleFirst:)];
    [self.view addGestureRecognizer:singleTap];
}

// 添加单击手势隐藏键盘
-(void)cancleFirst:(UITapGestureRecognizer *)singleTap
{
    [self.view endEditing:YES];
}

@end
